

#include "Public.h"


UR_FIFO_TY str_ur_fifo;

UR_FIFO_TY str_ur_fifo_2;

#if ENABLE_SEND_FIFO
UR_SEND_FIFO_TY str_ur_send_fifo;
#endif

#if ENABLE_SEND_FIFO_2
UR_SEND_FIFO_TY str_ur_send_fifo_2;
#endif

u8 counter = 0;

void ur_fifo_init(void)
{
	memset(&str_ur_fifo,0,sizeof(str_ur_fifo));
}


u8 ur_fifo_insert(u8 queue_data)
{
	if((str_ur_fifo.tail + 1) % QUEUE_BUF_SIZE == str_ur_fifo.head)
	{
		//input queue overflowing
		return 0;
	}
	str_ur_fifo.buf[str_ur_fifo.tail] = queue_data;

	str_ur_fifo.tail = (str_ur_fifo.tail + 1) % QUEUE_BUF_SIZE;

	return 1;
}


u8 ur_fifo_retrieve(u8 *queue_data)
{
	*queue_data = 0;
	
	if(str_ur_fifo.head == str_ur_fifo.tail)
	{
		// output queue is empty	
		return 0;
	}

	*queue_data = str_ur_fifo.buf[str_ur_fifo.head];
	
	str_ur_fifo.head = (str_ur_fifo.head + 1) % QUEUE_BUF_SIZE;

	return 1;
}

u8 ur_fifo_is_empty(void)
{
	if(str_ur_fifo.tail == str_ur_fifo.head)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}



#if ENABLE_UART2

void ur_fifo_init_2(void)
{
	memset(&str_ur_fifo_2,0,sizeof(str_ur_fifo_2));
}


u8 ur_fifo_insert_2(u8 queue_data)
{
	if((str_ur_fifo_2.tail + 1) % QUEUE_BUF_SIZE == str_ur_fifo_2.head)
	{
		//input queue overflowing
		uart2_send_byte(0xF0);
		return 0;
	}

	//uart2_send_byte(counter);
	
	str_ur_fifo_2.buf[str_ur_fifo_2.tail] = queue_data;

	str_ur_fifo_2.tail = (str_ur_fifo_2.tail + 1) % QUEUE_BUF_SIZE;

	return 1;
}


u8 ur_fifo_retrieve_2(u8 *queue_data)
{
	*queue_data = 0;
	
	if(str_ur_fifo_2.head == str_ur_fifo_2.tail)
	{
		// output queue is empty	
		return 0;
	}

	*queue_data = str_ur_fifo_2.buf[str_ur_fifo_2.head];
		
	str_ur_fifo_2.head = (str_ur_fifo_2.head + 1) % QUEUE_BUF_SIZE;

	return 1;
}

u8 ur_fifo_is_empty_2(void)
{
	if(str_ur_fifo_2.tail == str_ur_fifo_2.head)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
#endif

#if ENABLE_SEND_FIFO

void ur_send_fifo_init(void)
{
	memset(&str_ur_send_fifo,0,sizeof(str_ur_send_fifo));
}



u8 ur_send_fifo_insert(u8 *buf,u8 len)
{
	if((str_ur_send_fifo.tail + 1) % QUEUE_MAX_LEN == str_ur_send_fifo.head)
	{
		//input queue overflowing
		return 0;
	}

	str_ur_send_fifo.buf[str_ur_send_fifo.tail][0] = len; // the first element is len;

	memcpy(&str_ur_send_fifo.buf[str_ur_send_fifo.tail][1],buf,len);

	str_ur_send_fifo.tail = (str_ur_send_fifo.tail + 1) % QUEUE_MAX_LEN;

	return 1;
}





u8 ur_send_fifo_retrieve(u8 *buf)
{
	u8 len;
	
	if(str_ur_send_fifo.head == str_ur_send_fifo.tail)
	{
		// output queue is empty	
		return 0;
	}

	len = str_ur_send_fifo.buf[str_ur_send_fifo.head][0];

	memcpy(buf,&str_ur_send_fifo.buf[str_ur_send_fifo.head][1],len);
	
	str_ur_send_fifo.head = (str_ur_send_fifo.head + 1) % QUEUE_MAX_LEN;

	return len;
}


u8 ur_send_fifo_is_empty(void)
{
	if(str_ur_send_fifo.tail == str_ur_send_fifo.head)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

#endif



#if ENABLE_SEND_FIFO_2

void ur_send_fifo_init_2(void)
{
	memset(&str_ur_send_fifo_2,0,sizeof(str_ur_send_fifo_2));
}



u8 ur_send_fifo_insert_2(u8 *buf,u8 len)
{
	if((str_ur_send_fifo_2.tail + 1) % QUEUE_MAX_LEN == str_ur_send_fifo_2.head)
	{
		//input queue overflowing
		return 0;
	}

	str_ur_send_fifo_2.buf[str_ur_send_fifo_2.tail][0] = len; // the first element is len;

	memcpy(&str_ur_send_fifo_2.buf[str_ur_send_fifo_2.tail][1],buf,len);

	str_ur_send_fifo_2.tail = (str_ur_send_fifo_2.tail + 1) % QUEUE_MAX_LEN;

	return 1;
}





u8 ur_send_fifo_retrieve_2(u8 *buf)
{
	u8 len;
	
	if(str_ur_send_fifo_2.head == str_ur_send_fifo_2.tail)
	{
		// output queue is empty	
		return 0;
	}

	len = str_ur_send_fifo_2.buf[str_ur_send_fifo_2.head][0];

	memcpy(buf,&str_ur_send_fifo_2.buf[str_ur_send_fifo_2.head][1],len);
	
	str_ur_send_fifo_2.head = (str_ur_send_fifo_2.head + 1) % QUEUE_MAX_LEN;

	return len;
}


u8 ur_send_fifo_is_empty_2(void)
{
	if(str_ur_send_fifo_2.tail == str_ur_send_fifo_2.head)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

#endif

